package com.wangyadong.hobby.thread.miscellaneous;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.IntStream;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/311:23
 **/
public class Task implements Runnable {


    long n;
    String id;

    private long fib(long n) {
        if (n == 0) {
            return 0L;
        }
        if (n == 1) {
            return 0L;
        }
        return fib(n - 1) + fib(n - 2);
    }

    public Task(long n, String id) {
        this.n = n;
        this.id = id;
    }


    @Override
    public void run() {
        Date date = new Date();
        DateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
        long startTime = System.currentTimeMillis();
        date.setTime(startTime);
        System.out.println(String.format("Starting task " + id + " at " + simpleDateFormat.format(date)));
        long fib = fib(n);
        System.out.println("================" + fib);
        long endTime = System.currentTimeMillis();
        date.setTime(endTime);
        System.out.println("Ending task" + id + " at " + simpleDateFormat.format(date) + " after " + (endTime - startTime) + " millseconds");
    }

    public static void main(String[] args) {
        IntStream.range(0, 100).forEach(i -> {
            Task task = new Task(55, "李卫当官-" + i);
            Thread thread = new Thread(task);
            thread.start();
        });
    }
}
